interface DesignData {
  baseProductID: string;
  elements: DesignElement[];
  lastModified: number;
}

interface DesignElement {
  id: string;
  position: { x: number; y: number };
  transform: {
    scale: number;
    rotation: number;
  };
}

export class StorageService {
  private static instance: StorageService;
  private readonly STORAGE_KEY = 'tshirt_design_data';

  private constructor() {}

  static getInstance(): StorageService {
    if (!StorageService.instance) {
      StorageService.instance = new StorageService();
    }
    return StorageService.instance;
  }

  async saveDesign(data: DesignData): Promise<void> {
    try {
      localStorage.setItem(this.STORAGE_KEY, JSON.stringify(data));
    } catch (error) {
      console.error('保存设计数据失败:', error);
      throw new Error('保存失败');
    }
  }

  async loadDesign(): Promise<DesignData | null> {
    try {
      const data = localStorage.getItem(this.STORAGE_KEY);
      if (!data) return null;
      return JSON.parse(data);
    } catch (error) {
      console.error('加载设计数据失败:', error);
      return null;
    }
  }

  clearDesign(): void {
    localStorage.removeItem(this.STORAGE_KEY);
  }
} 